﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.CONCURSO.EN;
using MJM.CBD.Conn;

namespace MJM.CONCURSO.AD
{
    public static class PreguntaAD
    {
        public static int Insertar(PreguntaEN obj)
        {
            int Codigo = 0;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_i_T_WEB_PRECON_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_P_inID", obj.IDConcurso));
                cmd.Parameters.Add(new SqlParameter("@vPRECON_chDESPRE", obj.Pregunta.ToUpper()));
                cmd.Parameters.Add(new SqlParameter("@vPRECON_inNROPRE", obj.Numero));
                cmd.Parameters.Add(new SqlParameter("@vPRECON_inFLAELI", obj.Existe));
                SqlParameter prmID = new SqlParameter();
                prmID.Direction = ParameterDirection.ReturnValue;
                prmID.SqlDbType = SqlDbType.Int;
                cmd.Parameters.Add(prmID);

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                Codigo = Convert.ToInt32(prmID.Value);
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
                Codigo = 0;
            }
            return Codigo;
        }
        
        public static int ActualizaEstado(PreguntaEN obj)
        { 
             int Codigo = 1;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_u_T_WEB_PRECON_ActEstado", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vPRECON_P_inID", obj.ID));
                cmd.Parameters.Add(new SqlParameter("@vPRECON_inFLAELI", obj.Existe));
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
                Codigo = 0;
            }
            return Codigo;
        }

        public static List<PreguntaEN> Listar(PreguntaEN obj)
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_s_T_WEB_PRECON_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr;
            List<PreguntaEN> lst = new List<PreguntaEN>();
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_P_inID", obj.IDConcurso));
                cmd.Parameters.Add(new SqlParameter("@vPRECON_inFLAELI", obj.Existe));
                cmd.Connection.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    var objPreguntaEN = new PreguntaEN();
                    objPreguntaEN.ID = dr.GetInt32(dr.GetOrdinal("PRECON_P_inID"));
                    objPreguntaEN.IDConcurso = dr.GetInt32(dr.GetOrdinal("CONVEC_P_inID"));
                    objPreguntaEN.Numero = dr.GetInt32(dr.GetOrdinal("PRECON_inNROPRE"));
                    objPreguntaEN.Existe = dr.GetInt32(dr.GetOrdinal("PRECON_inFLAELI"));
                    objPreguntaEN.Pregunta = dr.GetString(dr.GetOrdinal("PRECON_chDESPRE"));
                    lst.Add(objPreguntaEN);
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }

            return lst;
        }
    }
}
